<template>
    <Dialog :title="title" :dialogVisible="data.visible" @close="close" :width="800" @confirm="confirm">
        <div class="form-content">
            <el-form :model="data.form" :rules="data.rules" label-width="100px" ref="formRef" label-position="top">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="data.form.name" placeholder="请输入名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="编码" prop="code">
                            <el-input v-model="data.form.code" placeholder="请输入编码" />
                        </el-form-item>
                    </el-col>



                    <el-col :span="12">
                        <el-form-item label="状态" prop="is_active">
                            <el-select v-model="data.form.is_active" placeholder="请选择状态">
                                <el-option label="启用" :value="true" />
                                <el-option label="禁用" :value="false" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="排序" prop="sort_order">
                            <el-input type="number" v-model="data.form.sort_order" placeholder="请输入排序" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="description">
                            <el-input type="textarea" v-model="data.form.description" placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

    </Dialog>
</template>

<script setup>
import Dialog from '@/components/dialog/index.vue'
import { useMessage } from '@/hooks/useMessage'
import { dictTypeGet, dictTypeCreate, dictTypeUpdate } from '@/api/dict/index.js'

const message = useMessage()


const emit = defineEmits(['success'])

const data = reactive({
    form: {
        name: '',
        code: '',
        description: '',
        is_active: true,
        sort_order: undefined
    },
    visible: false,
    type: 'add',
    rules: {
        name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        code: [
            { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        description: [
            { required: false, message: '请输入备注', trigger: 'blur' }
        ],
        is_active: [
            { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        sort_order: [
            { required: true, message: '请输入排序', trigger: 'blur' }
        ]
    },
    row: null
})

const title = computed(() => {
    return data.type === 'add' ? '新增字典' : '编辑字典'
})

const open = (type, row) => {
    data.visible = true
    data.type = type
    data.row = row
    if (row) {
        getDictTypeInfo(row.id)
    }
}

const getDictTypeInfo = async (id) => {
    const res = await dictTypeGet({ id })
    if (res.code == 200) {
        data.form = res.data
    }
}

const close = () => {
    data.visible = false

    data.form = {
        name: '',
        code: '',
        description: '',
        is_active: true,
        sort_order: undefined
    }
}

const formRef = ref(null)
const confirm = async () => {
    const valid = await formRef.value.validate()

    if (valid) {
        const api = data.type === 'add' ? dictTypeCreate : dictTypeUpdate
        const res = await api(data.form)
        if (res.code == 200) {
            message.alertMessage('操作成功', 'success')
            close()
            emit('success')
        } else {
            message.alertMessage(res.msg, 'error')
        }
    } else {
        message.alertMessage('请填写完整', 'error')
    }
}


defineExpose({
    open
})
</script>

<style scoped lang="scss">
.form-content {
    width: 100%;
    height: 100%;
    padding: 20px;
}
</style>